<template>
  <div class="all-titles-display">
    
    
    <!-- 称号概览卡片 -->
    <div class="titles-overview">
      <div class="overview-card" @click="showTitlesDialog = true">
        <div class="overview-icon">🏆</div>
        <div class="overview-content">
          <h4>称号管理</h4>
          <p>已解锁 {{ unlockedCount }}/{{ totalTitles }} 个称号</p>
          <div class="progress-bar">
            <div class="progress-fill" :style="{ width: unlockProgress + '%' }"></div>
          </div>
        </div>
        <div class="overview-arrow">→</div>
      </div>
      
      
    </div>
    
    <!-- 称号详情弹窗 -->
    <el-dialog
      v-model="showTitlesDialog"
      title="所有可用称号"
      width="80%"
      :close-on-click-modal="false"
      class="titles-dialog"
    >
      <div class="titles-grid">
        <div 
          v-for="title in allTitles" 
          :key="title.title"
          class="title-card"
          :class="{ 
            'unlocked': isTitleUnlocked(title), 
            'locked': !isTitleUnlocked(title),
            'current-title': title.title === currentTitle
          }"
          @click="handleTitleClick(title)"
        >
          <div class="title-header">
            <h4 class="title-name">
              <TitleBadge 
                :title="title.title"
                :level="title.requiredLevel"
                size="sm"
                :active="title.title === currentTitle"
              />
            </h4>
            <div class="title-badges">
              <span class="title-level">LV.{{ title.requiredLevel }}</span>
              <span v-if="title.title === currentTitle" class="current-title-badge">当前称号</span>
            </div>
          </div>
          
          <p class="title-description">{{ title.description }}</p>
          
          <div class="title-status">
            <el-tag 
              :type="isTitleUnlocked(title) ? 'success' : 'info'"
              size="small"
            >
              {{ isTitleUnlocked(title) ? '已解锁' : '未解锁' }}
            </el-tag>
            
            <div class="level-requirement" v-if="!isTitleUnlocked(title)">
              <el-icon><Lock /></el-icon>
              <span>需要等级 {{ title.requiredLevel }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="titles-summary">
        <div class="summary-stats">
          <div class="stat-item">
            <span class="stat-number">{{ totalTitles }}</span>
            <span class="stat-label">总称号</span>
          </div>
          <div class="stat-item">
            <span class="stat-number unlocked">{{ unlockedCount }}</span>
            <span class="stat-label">已解锁</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ lockedCount }}</span>
            <span class="stat-label">未解锁</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ unlockProgress }}%</span>
            <span class="stat-label">解锁进度</span>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import TitleBadge from './TitleBadge.vue'

const props = defineProps({
  allTitles: {
    type: Array,
    default: () => []
  },
  userTitles: {
    type: Array,
    default: () => []
  },
  currentTitle: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['titleChanged'])

// 弹窗状态
const showTitlesDialog = ref(false)

// 计算属性
const totalTitles = computed(() => props.allTitles.length)

// 判断称号是否解锁的逻辑
const isTitleUnlocked = (title) => {
  // 如果当前称号是用户正在使用的称号，认为是解锁的
  if (props.currentTitle && title.title === props.currentTitle) {
    console.log(`称号 "${title.title}" 是当前称号，已解锁`)
    return true
  }
  
  // 根据用户称号列表来判断
  if (props.userTitles && props.userTitles.length > 0) {
    const isUnlocked = props.userTitles.some(userTitle => {
      if (typeof userTitle === 'string') {
        // 如果userTitle是字符串，直接比较称号名称
        const unlocked = userTitle === title.title
        if (unlocked) {
          console.log(`称号 "${title.title}" 在用户称号列表中(字符串):`, userTitle)
        }
        return unlocked
      } else if (userTitle.title) {
        // 如果userTitle是对象且有title属性，比较title属性
        const unlocked = userTitle.title === title.title
        if (unlocked) {
          console.log(`称号 "${title.title}" 在用户称号列表中(对象):`, userTitle)
        }
        return unlocked
      } else if (userTitle.isUnlocked !== undefined) {
        // 如果userTitle有isUnlocked属性，使用该属性
        return userTitle.isUnlocked
      }
      return false
    })
    
    if (isUnlocked) {
      console.log(`称号 "${title.title}" 已解锁`)
    } else {
      console.log(`称号 "${title.title}" 未解锁，用户称号列表:`, props.userTitles)
    }
    
    return isUnlocked
  }
  
  console.log(`称号 "${title.title}" 未解锁，没有用户称号数据`)
  return false
}

const unlockedCount = computed(() => 
  props.allTitles.filter(title => isTitleUnlocked(title)).length
)

const lockedCount = computed(() => 
  props.allTitles.filter(title => !isTitleUnlocked(title)).length
)

const unlockProgress = computed(() => {
  if (totalTitles.value === 0) return 0
  return Math.round((unlockedCount.value / totalTitles.value) * 100)
})

// 处理称号点击
const handleTitleClick = (title) => {
  // 只有已解锁的称号才能点击
  if (isTitleUnlocked(title)) {
    // 如果点击的是当前称号，不需要更换
    if (title.title === props.currentTitle) {
      ElMessage.info('这已经是您当前的称号了')
      return
    }
    
    // 发出称号更换事件，但不刷新页面
    emit('titleChanged', title)
  } else {
    ElMessage.warning(`需要达到等级 ${title.requiredLevel} 才能解锁此称号`)
  }
}
</script>

<style scoped>
.all-titles-display {
  padding: 20px;
}

.titles-header {
  text-align: center;
  margin-bottom: 24px;
  padding: 20px;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border-radius: 16px;
  border: 1px solid rgba(226, 232, 240, 0.8);
}

/* 称号概览区域 */
.titles-overview {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 24px;
}

.overview-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
  border: 2px solid #e2e8f0;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.overview-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border-color: #3b82f6;
}

.overview-icon {
  font-size: 32px;
  margin-right: 16px;
  background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.overview-content {
  flex: 1;
}

.overview-content h4 {
  margin: 0 0 8px 0;
  color: #1e293b;
  font-size: 20px;
  font-weight: 700;
}

.overview-content p {
  margin: 0 0 12px 0;
  color: #64748b;
  font-size: 16px;
}

.progress-bar {
  width: 100%;
  height: 8px;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
  transition: width 0.6s ease;
}

.overview-arrow {
  font-size: 24px;
  color: #3b82f6;
  font-weight: 700;
  margin-left: 16px;
}

/* 当前称号显示 */
.current-title-display {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
  border: 2px solid #3b82f6;
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

.current-title-icon {
  font-size: 24px;
  margin-right: 12px;
  color: #f59e0b;
}

.current-title-info {
  display: flex;
  flex-direction: column;
}

.current-title-label {
  font-size: 12px;
  color: #64748b;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.current-title-name {
  font-size: 16px;
  color: #1e293b;
  font-weight: 700;
}

.titles-header h3 {
  margin: 0 0 12px 0;
  color: #1e293b;
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.titles-subtitle {
  margin: 0;
  color: #64748b;
  font-size: 18px;
  font-weight: 500;
  font-style: italic;
}

.titles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

/* 添加卡片进入动画 */
.title-card {
  animation: fadeInUp 0.6s ease-out;
}

.title-card:nth-child(1) { animation-delay: 0.1s; }
.title-card:nth-child(2) { animation-delay: 0.2s; }
.title-card:nth-child(3) { animation-delay: 0.3s; }
.title-card:nth-child(4) { animation-delay: 0.4s; }
.title-card:nth-child(5) { animation-delay: 0.5s; }

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.title-card {
  background: white;
  border: 2px solid #e5e7eb;
  border-radius: 16px;
  padding: 24px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  cursor: pointer;
}

.title-card.unlocked {
  cursor: pointer;
}

.title-card.locked {
  cursor: not-allowed;
}

.title-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: #e5e7eb;
  transition: all 0.4s ease;
}

.title-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.6s ease;
}

.title-card:hover::after {
  left: 100%;
}

.title-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}

.title-card.unlocked {
  border-color: #10b981;
  background: linear-gradient(135deg, #f0fdf4 0%, #ecfdf5 100%);
  box-shadow: 0 8px 20px rgba(16, 185, 129, 0.2);
}

.title-card.unlocked::before {
  background: linear-gradient(90deg, #10b981, #059669, #047857);
}

/* 当前称号卡片的特殊样式 */
.title-card.current-title {
  border-color: #3b82f6;
  border-width: 3px;
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
  box-shadow: 0 12px 30px rgba(59, 130, 246, 0.3);
  transform: scale(1.02);
  position: relative;
}

.title-card.current-title::before {
  background: linear-gradient(90deg, #3b82f6, #2563eb, #1d4ed8);
  height: 6px;
}

.title-card.current-title::after {
  background: linear-gradient(90deg, transparent, rgba(59, 130, 246, 0.1), transparent);
}

.title-card.current-title:hover {
  transform: scale(1.05);
  box-shadow: 0 16px 40px rgba(59, 130, 246, 0.4);
}

/* 当前称号的装饰元素 */
.title-card.current-title::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, #3b82f6, #2563eb, #3b82f6);
  border-radius: 18px;
  z-index: -1;
  animation: borderGlow 3s ease-in-out infinite;
}

@keyframes borderGlow {
  0%, 100% {
    opacity: 0.8;
  }
  50% {
    opacity: 1;
  }
}

.title-card.locked {
  border-color: #d1d5db;
  background: linear-gradient(135deg, #f9fafb 0%, #f3f4f6 100%);
  position: relative;
}

.title-card.locked::before {
  background: linear-gradient(90deg, #d1d5db, #9ca3af);
}

.title-card.locked::before {
  background: linear-gradient(90deg, #d1d5db, #9ca3af);
}

/* 添加锁的图标效果 */
.title-card.locked .title-name::before {
  content: '🔒';
  margin-right: 8px;
  font-size: 16px;
  opacity: 0.7;
}

.title-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.title-name {
  margin: 0;
  color: #374151;
  font-size: 24px;
  font-weight: 700;
  background: linear-gradient(90deg, #06b6d4 0%, #3b82f6 50%, #8b5cf6 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* 当前称号的标题特殊样式 */
.title-card.current-title .title-name {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
  font-weight: 800;
}

.title-badges {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* 当前称号徽章的特殊定位 */
.title-card.current-title .current-title-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  z-index: 10;
  transform: scale(1.1);
}

.title-level {
  background: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);
  color: white;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.25);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.current-title-badge {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  color: white;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  border: 2px solid #bfdbfe;
  animation: pulse 2s infinite;
  position: relative;
}

.current-title-badge::before {
  content: '⭐';
  margin-right: 4px;
  font-size: 12px;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(59, 130, 246, 0.6);
  }
}

.title-description {
  margin: 0 0 20px 0;
  color: #4b5563;
  font-size: 16px;
  line-height: 1.6;
  min-height: 44px;
  font-style: italic;
  text-align: justify;
  padding: 12px;
  background: rgba(249, 250, 251, 0.8);
  border-radius: 8px;
  border-left: 3px solid #e5e7eb;
}

.title-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  border: 1px solid rgba(229, 231, 235, 0.5);
}

.level-requirement {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #6b7280;
  font-size: 14px;
  font-weight: 500;
  padding: 6px 10px;
  background: rgba(243, 244, 246, 0.8);
  border-radius: 6px;
}

.titles-summary {
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border-radius: 16px;
  padding: 28px;
  border: 1px solid rgba(226, 232, 240, 0.8);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.summary-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 24px;
  text-align: center;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  border: 1px solid rgba(226, 232, 240, 0.5);
  transition: all 0.3s ease;
}

.stat-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.stat-number {
  font-size: 36px;
  font-weight: 800;
  color: #1e293b;
  margin-bottom: 6px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.stat-number.unlocked {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.stat-label {
  font-size: 13px;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  font-weight: 600;
}

/* 弹窗样式 */
.titles-dialog .el-dialog__body {
  padding: 20px;
}

.titles-dialog .titles-grid {
  margin-bottom: 24px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .titles-grid {
    grid-template-columns: 1fr;
  }
  
  .summary-stats {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .overview-card {
    padding: 16px;
  }
  
  .overview-icon {
    font-size: 24px;
    margin-right: 12px;
  }
  
  .overview-content h4 {
    font-size: 16px;
  }
  
  .overview-content p {
    font-size: 12px;
  }
}
</style>
